<template>
    <div>
        <el-container class="form-container">
            <el-card class="box-card" shadow="never">
                <div slot="header" class="clearfix">
                    <span>基本信息</span>
                    <el-button type="primary" size="mini" style="float:right;padding: 6px 9px;" @click="submitForm">保存</el-button>
                </div>
                <div >
                   <el-form  :rules="rules"  ref="form"   :model="form" size="small" label-width="120px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item  label="名称"  prop="fdName">
                                    <el-input class="input" v-model="form.fdName"  ></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item  label="编号"  prop="fdNo">
                                    <el-input class="input" v-model="form.fdNo"  ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item  label="所属部门"  prop="fdParentName">
                                    <fancy-address v-model="form.fdParentName" :id.sync="form.fdParentId"  orgType="DEPT"  />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item  label="是否有效" prop="fdIsAvaliable">
                                    <el-switch v-model="form.fdIsAvaliable"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                   </el-form>
                </div>
            </el-card>
        </el-container>
    </div>
</template>
<script>
import dateUtils from '@/utils/dateUtils';
import formUtils from '@/utils/formUtils';
export default {
    name: "SysOrgPostAdd",
    components: {
    },
    data() {
        return {
            form: {
                fdIsAvaliable:true,
                fdName:'',
                fdNo:'',
                fdId:'',
                fdParentId:'',
                fdParentName:''
            },
            rules:{
                fdName :[{required: true, message: '请输入名称', trigger: 'blur'}]
	        }
        }
    },
    created() {
        this.initParam();
    },
    methods: {
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.fullscreenLoading = true;
                    let forms = formUtils.convertToModel(this.form);
                    this.$put('/api/sys/org/sys_org_post',forms).then((res) => {
                        this.fullscreenLoading = false;
                        if(res.status==1){
                            this.$message({
                                message: res.message,
                                type: 'success',
                                onClose:()=>{
                                    this.$common.closeTag();
                                }
                            });
                        }else{
                            this.$message.error(res.message);
                        }
                    }).catch(() =>{
                        this.fullscreenLoading = false;
                    });

                }
            });
        },
        initParam() {
            let fdId = this.$route.query.id;
            if(formUtils.isNotEmpty(fdId)){
                this.fullscreenLoading = true;
                this.$fetch('/api/sys/org/sys_org_post',{
                    id:fdId
                }).then((res) => {
                    console.log(res.data);
                    formUtils.convertToForm(res.data,this.form);
                    console.log(this.form);
                    this.fullscreenLoading = false;
                });
            }
            let fdParentId = this.$route.query.fdParentId;
            if(formUtils.isNotEmpty(fdParentId)){
                this.fullscreenLoading = true;
                this.$fetch('/api/sys/org/sys_org_dept',{
                    id:fdParentId
                }).then((res) => {
                    this.fullscreenLoading = false;
                    this.form.fdParentId = res.data.fdId;
                    this.form.fdParentName = res.data.fdName;
                });
            }
        }
    }
}
</script>
<style scoped >
@import "~@/assets/css/form.css";
</style>